#include "groupsessiondetailwidget.h"

#include "Debug.h"

GroupSessionDetailWidget::GroupSessionDetailWidget(QWidget *parent):
    QDialog(parent), m_container(nullptr), m_groupNameEdit(nullptr)
{
    this->setFixedSize(250, 400);
    this->setWindowTitle("群聊详情");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setStyleSheet("QWidget { background-color: white; }"
                        "QScrollArea { border: none; }"
                        "QScrollBar:vertical { width: 2px; background-color: rgb(255, 255, 255); }"
                        "QScrollBar::handle:vertical {background-color: rgb(205, 205, 205);}"
                        "QScrollBar:horizontal { height: 0px;}"
                        "QPushButton { border: none; border-radius: 5px; }"
                        "QPushButton:pressed { background-color: #C6C6C6; }");

    // 1.整体垂直布局
    QVBoxLayout* vLayout = new QVBoxLayout();
    vLayout->setContentsMargins(0, 10 ,0, 20);
    vLayout->setSpacing(5);
    vLayout->setAlignment(Qt::AlignTop | Qt::AlignCenter);
    this->setLayout(vLayout);

    // 2.1 设置滚动区
    QScrollArea* scrollArea = new QScrollArea();
    scrollArea->setWidgetResizable(true);
    scrollArea->setFixedSize(210, 280);
    m_container = new QWidget();
    scrollArea->setWidget(m_container);
    // 2.2 设置容器里的网格布局
    QGridLayout* gLayout = new QGridLayout();
    gLayout->setContentsMargins(0, 0, 0, 0);
    gLayout->setSpacing(0);
    m_container->setLayout(gLayout);
    // 2.3 往网格布局中添加一个固定元素“添加”
    AvatarItem* addAvatar = new AvatarItem(QIcon(":/resource/image/cross.png"), "添加");
    gLayout->addWidget(addAvatar, 0, 0, Qt::AlignTop | Qt::AlignLeft);
    connect(addAvatar->getAvatarBtn(), &QPushButton::clicked, this, [=]{
        ChooseFriendDialog* chooseFriendDialog = new ChooseFriendDialog();
        chooseFriendDialog->exec();
    });
    vLayout->addWidget(scrollArea);

    // 3.群聊名称标签
    QLabel* groupNameLabel = new QLabel("群聊名称");
    groupNameLabel->setStyleSheet("QLabel { font-size: 12px; font-weight: 800; }");
    groupNameLabel->setFixedHeight(30);
    // groupNameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    vLayout->addWidget(groupNameLabel);

    // 4.实际群聊名称+编辑按钮
    QHBoxLayout* hLayout = new QHBoxLayout();
    m_realGroupNameLabel = new QLabel();
    m_realGroupNameLabel->setStyleSheet("QLabel { font-size: 12px; color: gray; }");
    QPushButton* editBtn = new QPushButton();
    editBtn->setIcon(QIcon(":/resource/image/modify.png"));
    editBtn->setIconSize(QSize(15, 15));
    editBtn->setFixedSize(15, 15);
    editBtn->setCursor(Qt::PointingHandCursor);

    hLayout->addWidget(m_realGroupNameLabel, 0, Qt::AlignLeft);
    hLayout->addWidget(editBtn, 0, Qt::AlignRight);
    vLayout->addLayout(hLayout);

    // 5.退出群聊按钮
    QPushButton* exitBtn = new QPushButton("退出群聊");
    exitBtn->setFixedSize(200, 30);
    exitBtn->setStyleSheet("QPushButton { color: red; border: none; background-color: rgb(235, 235, 235); border-radius: 5px; }"
                           "QPushButton:pressed { background-color: #C6C6C6; }");
    exitBtn->setCursor(Qt::PointingHandCursor);
    vLayout->addWidget(exitBtn);

#if TEST_UI
    realGroupNameLabel->setText("测试群聊名称");
    for (int i = 0; i < 20; i++) {
        AvatarItem* avatarItem = new AvatarItem(QIcon(":/resource/image/defaultAvatar2.jpg"), "哈哈哈哈哈哈哈或或或");
        this->addMember(avatarItem);
    }
#endif

    // 8. 从服务器加载数据
    initData();
}
void GroupSessionDetailWidget::initData()
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    connect(dataCenter, &model::DataCenter::getMemberListDone, this, &GroupSessionDetailWidget::initMemberList);
    dataCenter->getMemberListAsync(dataCenter->getCurrentChatSessionId());
}

void GroupSessionDetailWidget::addMember(AvatarItem *avatar)
{
    if(m_col >= 4){
        m_col = 0;
        ++m_row;
    }
    QGridLayout* gLayout = dynamic_cast<QGridLayout*>(m_container->layout());
    gLayout->addWidget(avatar, m_row, m_col);
    ++m_col;
}

void GroupSessionDetailWidget::initMemberList(const QString& chatSessionId)
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    auto memberList = dataCenter->getMemberList(chatSessionId);
    if(memberList == nullptr){
        LOG() << "获取的成员列表为空! chatSessionId=" << chatSessionId;
        return ;
    }
    for(const auto& m : *memberList){
        auto avatar = new AvatarItem(m.m_avatar, m.m_nickName);
        addMember(avatar);
    }
    m_realGroupNameLabel->setText("（未定义）");
}
